package com.ouken.asynTask;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import com.gen.com.GameRoomManagerActivity;
import com.gen.com.GameRoomPlayerActivity;
import com.gen.tool.AlertDialog_PG;
import com.ouken.com.LoginActivity;
import com.ouken.com.R;
import com.ouken.tool.ConnectServer;
import com.ouken.tool.MyApp;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AsynCreateRoom extends AsyncTask<String, TextView, String> {

	private Context context;
	private WifiManager wifi;
	private List params;
	private String httpUrl;
	private MyApp appState;
	//private ProgressDialog dialog;
	private String roonName;
	private String pwd;
	
	private AlertDialog_PG PGDialog;
	private AlertDialog PGnow;
	

	public AsynCreateRoom(Context context, MyApp appState, String roomName, String pwd) {
		super();
		this.context = context;
		this.wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
		this.params = new ArrayList();
		this.httpUrl = "http://"+ConnectServer.getServerAdd()+"/createroom";
		this.appState = appState;
		//this.dialog = new ProgressDialog(context);
		this.roonName = roomName;
		this.pwd = pwd;
		
		PGDialog = new AlertDialog_PG(context,"Please wait \nwhile loading...");
	}
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		
		System.out.println("AsynCreateRoom onpreexcute");

		final AlertDialog PGnow1 = PGDialog.AlertDialog_PG_show();
		PGnow = PGnow1;
        /*dialog.setMessage("Please wait while loading...");
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        dialog.show();*/
        
		params.add(new BasicNameValuePair("email", appState.getUserEmail().toString()));
		params.add(new BasicNameValuePair("roomname", roonName.toString()));
		if(pwd.equals("") || pwd == null){
		 params.add(new BasicNameValuePair("passwd", "?????????????"));
		}else
		{
			params.add(new BasicNameValuePair("passwd", pwd.toString()));
		}
		params.add(new BasicNameValuePair("mac", wifi.getConnectionInfo().getMacAddress()));
		params.add(new BasicNameValuePair("ParamEnd", "ParamEndParamEndParamEnd"));
		
	}
	@Override
	protected String  doInBackground(String...strings ) {
		System.out.println("AsynCreateRoom doInBackground");
		return ConnectServer.connect(httpUrl,params);
		
	}

	@Override
    protected void onPostExecute(String result) {
		System.out.println("AsynCreateRoom onPostExecute");
		
		final AlertDialog PGnow_c = PGnow;
		
		if(result == null){
			PGnow_c.dismiss();
			 //dialog.dismiss();
			 AlertDialog alertDialog =
					new AlertDialog.Builder(context)
	                .setIcon(R.drawable.alert_dialog_icon)
	                .setTitle("ERROR!")
	                .setMessage(R.string.ERROR_NETWORKOFF)
	                .setPositiveButton("OK",null)
	                .create();
				
			alertDialog.show();
			
			return;
		}
		String[] resultParams =result .split(",");
		
		if(resultParams[0].toString().equals("OK")){
			
			/****send the roomname to the next activity  and start that activity***/
			System.out.println("enter room success!!");
			PGnow_c.dismiss();
			//dialog.dismiss();
			
			appState.setRoomName(this.roonName);
    		appState.setMem("1", "5");
			
			Intent intent = new Intent(context,GameRoomManagerActivity.class);
			context.startActivity(intent);
			
			((Activity)context).finish();
			
			
			
		}else{
			PGnow_c.dismiss();
			//dialog.dismiss();
			AlertDialog alertDialog =
				new AlertDialog.Builder(context)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle("ERROR!")
                .setMessage(resultParams[1])
                .setPositiveButton("OK",null)
                .create();
			
				alertDialog.show();
		}
		
    }
	

	

	
	
	
}
